//
//  RewardView.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/11/11.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "RewardView.h"

@implementation RewardView

-(instancetype)initWithArray:(NSDictionary *)dic andFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.dic=dic;
        [self rankView];
    }
    return self;
}
-(void)rankView{
    UIImage *image = [UIImage imageNamed:@"image_textbox"];
    
    // 设置左边端盖宽度
    NSInteger leftCapWidth = image.size.width * 0.5;
    // 设置上边端盖高度
    NSInteger topCapHeight = image.size.height * 0.5;
    
    UIImage *newImage = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
    
    self.background=[[UIImageView alloc]init];
    self.background.image=newImage;
   // self.background.frame=CGRectMake(0, 0, MainW, 60);
    [self addSubview:self.background];
    
    self.headImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.dic[@"avatar"]]] placeholderImage:[UIImage imageNamed:@"img_fail_monk"]];
    [self.background addSubview:self.headImage];
    ViewRadius(self.headImage, 20);
    NSString*steing=[NSString stringWithFormat:@"%@",self.dic[@"nickName"]];
    CGSize size=[steing sizeWithAttributes:@{NSFontAttributeName:FontAndStyle12 }];
    
    
    self.nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(self.headImage.bsg_right+10, 10, size.width, 15)];
    self.nameLabel.text=[NSString stringWithFormat:@"%@",self.dic[@"nickName"]];
    
    self.nameLabel.font=FontAndStyle12;
    self.nameLabel.textColor=Red_Color;
    [self.background addSubview:self.nameLabel];
    
    NSMutableArray*array=[NSMutableArray array];
    for (NSDictionary*dic in self.dic[@"prayInfoattr"]) {
        [array addObject:dic[@"name"]];
    }
    CGSize size1=[[array componentsJoinedByString:@" "] sizeWithAttributes:@{NSFontAttributeName:FontAndStyle12 }];
    
    
    self.labelling=[[UILabel alloc]initWithFrame:CGRectMake(self.nameLabel.bsg_right+8, 10, size1.width, 15)];
    self.labelling.text=[array componentsJoinedByString:@" "];
    
    self.labelling.font=FontAndStyle12;
    self.labelling.textColor=TextColor6;
    [self.background addSubview:self.labelling];
    
    CGSize size2=[self.dic[@"prayInfo"] sizeWithAttributes:@{NSFontAttributeName:FontAndStyle12 }];
    if (size2.width>(MainW-130)) {
        size2.width=MainW-130;
    }
    self.content=[[UILabel alloc]initWithFrame:CGRectMake(self.headImage.bsg_right+10, self.nameLabel.bsg_bottom+10, size2.width, 15)];
    self.content.text=[NSString stringWithFormat:@"%@",self.dic[@"prayInfo"]];
    
    self.content.font=FontAndStyle12;
    self.content.textColor=TextColor3;
    [self.background addSubview:self.content];
    CGFloat with=size1.width+size.width+10;
    
    self.imgeview=[[UIImageView alloc]init];
    if (size2.width>with) {
        self.imgeview.frame=CGRectMake(self.content.bsg_right+10, 15, 30, 30);
    }else{
        self.imgeview.frame=CGRectMake(self.labelling.bsg_right+10, 15, 30, 30);
    }
    [self.imgeview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.dic[@"imageUrl"]]]];
    [self.background addSubview:self.imgeview];
    ViewRadius(self.imgeview, 3);
    if ([NSString stringWithFormat:@"%@",self.dic[@"imageUrl"]]==nil) {
        self.background.frame=CGRectMake(10, 10, CGRectGetMaxX(self.imgeview.frame)-50, 60);
    }else{
        self.background.frame=CGRectMake(10, 10, CGRectGetMaxX(self.imgeview.frame)+10, 60);
    }
    self.selectBtn=[[UIButton alloc]init];
    self.selectBtn.frame=CGRectMake(0, 0, self.background.bsg_width+10, self.background.bsg_height+10);
    [self.selectBtn addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
    //self.selectBtn.backgroundColor=[UIColor redColor];
    [self addSubview:self.selectBtn];
    
    NSLog(@"self.background_____%f",self.background.frame.origin.y);
}
-(void)selectClick:(UIButton *)sender{
    sender.selected=YES;
    [_delegate selectBtnTitleandBtn:_dic];
}
@end
